package org.jader.bolao.gui.swing;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.io.File;
import java.io.FileFilter;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class XSLTFileChooser extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel southPanel = null;
	private JPanel centerPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JLabel dialogLabel = null;
	private JScrollPane listScrollPane = null;
	private JList templatesList = null;

	/**
	 * @param owner
	 */
	public XSLTFileChooser(Frame owner) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLocationRelativeTo(getParent());
		this.setContentPane(getJContentPane());
		this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getSouthPanel(), BorderLayout.SOUTH);
			jContentPane.add(getCenterPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes southPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getSouthPanel() {
		if (southPanel == null) {
			southPanel = new JPanel();
			southPanel.setLayout(new FlowLayout());
			southPanel.add(getOkButton(), null);
			southPanel.add(getCancelButton(), null);
		}
		return southPanel;
	}

	/**
	 * This method initializes centerPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getCenterPanel() {
		if (centerPanel == null) {
			dialogLabel = new JLabel();
			dialogLabel.setText("Selecione o template a ser utilizado:");
			centerPanel = new JPanel();
			centerPanel.setLayout(new BoxLayout(getCenterPanel(), BoxLayout.Y_AXIS));
			centerPanel.add(dialogLabel, null);
			centerPanel.add(getListScrollPane(), null);
		}
		return centerPanel;
	}

	/**
	 * This method initializes okButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancelar");
			cancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getTemplatesList().setSelectedIndex(-1);
					dispose();
				}
			});
		}
		return cancelButton;
	}

	/**
	 * This method initializes listScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getListScrollPane() {
		if (listScrollPane == null) {
			listScrollPane = new JScrollPane();
			listScrollPane.setViewportView(getTemplatesList());
		}
		return listScrollPane;
	}

	/**
	 * This method initializes templatesList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getTemplatesList() {
		if (templatesList == null) {
			// Get all xslt files from org.jader.bolao.text.html
			String dirPath = getClass().getResource("../../text/html").getPath();
			File[] files = new File(dirPath).listFiles(new FileFilter() {
				@Override
				public boolean accept(File arg0) {
					if (arg0.getName().endsWith(".xslt")) return true;
					return false;
				}
			});
			// Create list with found files
			templatesList = new JList(files);
			templatesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		return templatesList;
	}

	public File getSelectedFile() {
		if (getTemplatesList().getSelectedIndex() > -1)
			return (File) getTemplatesList().getSelectedValue();
		return null;
	}

}  //  @jve:decl-index=0:visual-constraint="99,33"
